/**
 * 公用的导航菜单
 */
function getIndexNavigation(
    {
        currUrl,
        roleId = null,
        callback = Function()
    }
) {
    const $widget = $(`<ul class="nav"></ul>`);

    queryCurrUserMenuList({
        roleId
    }, ({Code, Message, Result}) => {
        if (Code !== 10000) {
            return location.href = "/admin/login.html?" + $.param({
                msg: Message,
                back: location.href
            });
        }

        const menuList = Result || [];

        //父类的导航菜单
        const parentMenuList = menuList.filter(({parentMenuId}) => !parentMenuId);
        if (!currUrl && parentMenuList[0].url) {
            currUrl = parentMenuList[0].url;
        }

        //子菜单
        const submenuMenuList = [];
        parentMenuList.forEach(({menuId}) => {
            menuList.forEach(nav => {
                const {parentMenuId} = nav;
                if (parentMenuId === menuId) {
                    if (!currUrl && nav.url) {
                        currUrl = nav.url;
                    }
                    submenuMenuList.push(nav)
                }
            });
        });

        if (!currUrl) {
            if (parentMenuList[0].url) {
                currUrl = parentMenuList[0].url;
            } else {

            }
        }

        //先写入父菜单的html
        parentMenuList.forEach(nav => {
            const $li = getLi(nav)
            nav.$dom = $li;
            if (nav.url && currUrl.indexOf(nav.url) !== -1) {
                $li.click();
            }
        });


        //杠出子类的菜单
        submenuMenuList.forEach(nav => {
            const {parentMenuId} = nav;
            const parentList = parentMenuList.filter(parentNav => {
                return parentNav.menuId === parentMenuId;
            });

            //找不到父类的，剔除
            if (!parentList || parentList.length === 0) {
                return;
            }

            const $parentNav = parentList[0].$dom;

            let $ul = $parentNav.find("ul");
            if (!$ul || !$ul.length) {
                $ul = $(`<ul class="nav-second-level c-hide"></ul>`);
                $parentNav.append($ul);
            }

            const $li = getLi(nav);
            nav.$dom = $li;
            $ul.append($li);

            if (nav.url && currUrl.indexOf(nav.url) !== -1) {
                $li.click();
                $li.parent("ul").show();
            }

        });

        parentMenuList.forEach(nav => {
            const {$dom} = nav;
            $widget.append($dom);
        })

    });


    return $widget;

    function getLi(
        {
            menuId,
            title,
            url,
            iconClass,
            iconImgUrl,

        }
    ) {

        let $iconDom = "";
        if (iconClass) {
            $iconDom = `<i class="c-f20 mr-12 ${iconClass}"></i>`;
        } else if (iconImgUrl) {
            $iconDom = `<img class="mr-12" src="${iconImgUrl}" style="width: 25px;height: 25px"  alt="">`;
        }

        const $li = $(`
            <li data-name="${menuId}" data-url="${url || ""}">
                <a href="javascript:">
                    ${$iconDom}
                    <span class="nav-label">${title}</span>
                </a>
            </li>
        `);

        $li.on("click", (e) => {
            e.stopPropagation();
            if (url) {
                callback($.sUrl(url));
            }

            //有子类，子类显示隐藏切换
            if ($li.children("ul").length) {
                $li.children("ul").toggle();
            }

            //所有的选中全部清除
            $widget.find(".active").removeClass("active");

            //自己和父类加上选中
            $li.addClass("active");
            $li.parents("li").addClass("active");
        });

        return $li;
    }


    function queryCurrUserMenuList(param, callback = Function()) {
        return $.sApi({
            data: param,
            url: "/api/Menu/queryCurrUserMenuList",
        }, callback)
    }
}